<?php

namespace Org\Util;

//定义短信模板id
//国际短信验证码模板ID
define('YP_VCODE', 1255501);
define('YP_REGISTER', 1695422);
define('NEW_SMS_TEMPLETE_NOT_REGISTER', 1821804);
define('NEW_SMS_TEMPLETE_DEALER_HOTEL_PASS', 1821800);
define('NEW_SMS_TEMPLETE_APPLY_PASS', 1821792);
define('NEW_SMS_TEMPLETE_APPLY_REFUSE', 1821794);
define('NEW_SMS_TEMPLETE_DEALER_REFUSE', 1821798);
define('NEW_SMS_TEMPLETE_DEALER_PASS', 1821796);
define('NEW_SMS_TEMPLETE_ADDHOTEL_MESSAGE', 1821816);


/**
 * 云片网络短信发送工具类
 * @modify songanwei 2016-03-02
 */
class yunPian {

    private $apiKey = 'e2ce22fafd876da1deecddba13cf5ae6';
    private $fileName;

    /**
     * 打印日志错误
     * @param string $log 错误日志信息
     */
    private function showLog($log) {
        $this->fileName = LOG_PATH . 'yunpian_log.txt';
        //检查文件是否存在
        if (!file_exists($this->fileName)) {
            //创建文件
        }
        $this->handle = fopen($this->fileName, "a");
        fwrite($this->handle, date('Y-m-d H:i:s') . " " . $log . "\r\n");
        fclose($this->handle);
    }

    /**
     * 模板发送短信
     * @modify songanwei 2016-03-02
     * @param array $data 发送短信数组
     * @return type
     */
    protected function tplSend($data) {
        $ch = curl_init();
        //设置验证方式
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
        //设置返回结果为流
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //设置超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        //设置通信方式
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/tpl_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $jsonData = curl_exec($ch);
        $array = json_decode($jsonData, true);
        return $array;
    }

    /**
     * 发送短信验证码
     * @modify songanwei 2016-03-02
     * @param string $areaCode 国际区号
     * @param string $mobile 国外手机号
     * @param integer $vcode 验证码
     * @return boolean
     */
    public function sendVcode($areaCode, $mobile, $vcode) {
        $mobile = '+' . $areaCode . $mobile;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => YP_VCODE, 'tpl_value' => urlencode('#code#') . '=' . urlencode($vcode), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 注册成功发送密码通知
     * @modify wenhui 2017-01-17
     * @param string $areaCode 国际区号
     * @param string $mobile 国外手机号
     * @param integer $vcode 验证码
     * @return boolean
     */
    public function sendRegisterSMS($areaCode, $mobile, $pwd) {
        $mobile = '+' . $areaCode . $mobile;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => YP_REGISTER, 'tpl_value' => urlencode('#password#') . '=' . urlencode($pwd), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 发送用户未注册开通客栈申请通知
     * @modify songanwei 2016-01-04
     * @param string $mobile 发送到此手机号码
     * @param string $password 初始密码
     * @return mixed 发送结果
     */
    public function sendNotReg($areaCode, $account, $password) {
        $mobile = '+' . $areaCode . $account;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => NEW_SMS_TEMPLETE_NOT_REGISTER, 'tpl_value' => urlencode('#account#') . '=' . urlencode($account) . '&' . urlencode('#password#') . '=' . urlencode($password), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 发经销商客栈申请通过短信通知
     * @modify zhangzhen 2017-01-09
     * @param type $account  发送到此手机号
     * @param type $hotelName 客栈名称
     * @param type $userName 用户名
     * @return type
     */
    public function sendDealerHotelPass($areaCode, $account, $hotelName, $userName) {
        $mobile = '+' . $areaCode . $account;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => NEW_SMS_TEMPLETE_DEALER_HOTEL_PASS, 'tpl_value' => urlencode('#inn#') . '=' . urlencode($hotelName) . '&' . urlencode('#contact#') . '=' . urlencode($userName), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 发送用户未注册开通客栈申请通知
     * @modify songanwei 2016-01-04
     * @param string $mobile 发送到此手机号码
     * @param string $password 初始密码
     * @return mixed 发送结果
     */
    public function yunPiansendNotReg($areaCode, $account, $password) {
        $mobile = '+' . $areaCode . $account;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => NEW_SMS_TEMPLETE_APPLY_PASS, 'tpl_value' => urlencode('#account#') . '=' . urlencode($account) . '&' . urlencode('#password#') . '=' . urlencode($password), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 发送客栈申请拒绝短信通知
     * @modify songanwei 2016-03-23
     * @param type $mobile  发送到此手机号
     * @param type $hotelName 客栈名称
     * @return type
     */
    public function sendRefuse($areaCode, $account, $hotelName) {
        $mobile = '+' . $areaCode . $account;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => NEW_SMS_TEMPLETE_APPLY_REFUSE, 'tpl_value' => urlencode('#inn#') . '=' . urlencode($hotelName), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 发送经销商申请通过短信通知
     * @modify zhangzhen 2017-01-09
     * @param type $mobile  发送到此手机号
     * @param type $password 密码
     * @return type
     */
    public function sendDealerPass($areaCode, $account, $password) {
        $mobile = '+' . $areaCode . $account;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => NEW_SMS_TEMPLETE_DEALER_PASS, 'tpl_value' => urlencode('#account#') . '=' . urlencode($account) . '&' . urlencode('#password#') . '=' . urlencode($password), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 发送经销商申请拒绝短信通知
     * @modify zhangzhen 2017-01-09
     * @param type $mobile  发送到此手机号
     * @return type
     */
    public function sendDealerRefuse($areaCode, $account) {
        $mobile = '+' . $areaCode . $account;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => NEW_SMS_TEMPLETE_DEALER_REFUSE, 'tpl_value' => urlencode('#account#') . '=' . urlencode($account), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

    /**
     * 发送客户添加客栈信息给经销商
     * @modify zhangzhen 2017-01-12
     * @param string $mobile  发送手机号
     * @param string $password 发送密码
     * @return type
     */
    public function sendAddHotelToDealer($areaCode,$account,$userName,$userMobile,$hotelName) {
        $mobile = '+' . $areaCode . $account;
        //封装数据,对tpl_value参数值进行编码
        $data = array('tpl_id' => NEW_SMS_TEMPLETE_ADDHOTEL_MESSAGE, 'tpl_value' => urlencode('#customer#') . '=' . urlencode($userName) . '&' . urlencode('#mobile#') . '=' . urlencode($userMobile) . '&' . urlencode('#inn#') . '=' . urlencode($hotelName), 'apikey' => $this->apiKey, 'mobile' => $mobile);
        $result = $this->tplSend($data);
        if ($result['code'] == 0) {
            return true;
        }
        //添加日志
        $this->showLog($mobile . ":[" . $result['code'] . "]" . $result['msg']);
        return false;
    }

}
